﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using WCYCReader.Utility;
using WCYCReader.ViewModels;
using WCYCReader.Main;
using WCYCReader.Models;

namespace WCYCReader.Pages
{
    public partial class BookDetailPage : PhoneApplicationPage
    {
        protected BookDetail_VM bdvm = new BookDetail_VM();

        public BookDetailPage()
        {
            this.InitializeComponent();
        }

        private void ContentGrid_Loaded(object sender, RoutedEventArgs e)
        {
            App.QQReader_prePageId = App.QQReader_pageId;
            App.QQReader_pageId = "QQReader_DetailPage";
        }

        private void ContentGrid_Unloaded(object sender, RoutedEventArgs e)
        {
        }

        private void downLoad_Click(object sender, RoutedEventArgs e)
        {
            (sender as Button).IsEnabled = false;
            if ("/Resource/DefaultBookBackground.jpg" != this.bdvm.T_BookDetail()[0].str_Cover_M)
            {
                App.DownloadList.AddDownloadImage(this.bdvm.T_BookDetail()[0].str_Cover_M);
            }
            else
            {
                this.bdvm.T_BookDetail()[0].str_ShowTitle_M = this.bdvm.T_BookDetail()[0].str_Title_M;
            }
            App.LocalBook.AddBookList(this.bdvm.T_BookDetail()[0]);
            SettingHelper.SaveInToSettings(this.bdvm.T_BookDetail()[0].str_Id_M, true);
            App.DownloadList.AddDownloadList(this.bdvm.T_BookDetail()[0].str_DownUrl_M, this.bdvm.T_BookDetail()[0].str_Id_M);
            Prompt.PromptMessage("提示：当前书籍加入下载队列！");
        }

        private void downLoad_Loaded(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
            if (App.QQReader_StorageFile.IsFileExists(this.bdvm.T_BookDetail()[0].str_Id_M.ToString()))
            {
                button.Content = "已下载";
                button.IsEnabled = false;
            }
            if (SettingHelper.IsSettings(this.bdvm.T_BookDetail()[0].str_Id_M))
            {
                button.IsEnabled = false;
            }
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            if (this.bdvm.T_BookDetail().Count < 1)
            {
                this.bdvm.T_BookDetail().Add(SettingHelper.GetValueFromSettings("BookDetail") as BookList_M);
                this.bookDetailListBox.ItemsSource = this.bdvm.T_BookDetail();
            }
        }
    }
}
